home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7640 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: sizeof dymanic arrays
  5. Date: Sat, 24 Feb 1996 19:21:35 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4gnoer$t4j@news.halcyon.com>
  8. References: <312BE6BA.3725@byu.edu> <312C9863.372D@bridge.bst.bls.com>
  9. NNTP-Posting-Host: blv-pm11-ip3.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Just Another Net User <gary.smith3@bridge.bst.bls.com> wrote:
  13.  
  14. >> I have a problem with getting the size of a dynamic array.
  15. >>
  16. >> {
  17. >>     char* temp;
  18. >>     temp=new char[10];
  19. >>     cout << sizeof temp;
  20. >> }
  21. >>
  22. >> I get 4 back for this.  Which is the size of the pointer, not the array.
  23.  
  24. >Correct. Also sizeof *temp returns 1 (the size of 1 char).
  25.  
  26. >>  Does anyone know how to get the number of elements in a dynamic array?
  27.  
  28. >Yes, you already know. You did tell new how many elements you wanted in
  29. >the first place (10 in your example). 
  30.  
  31.  
  32. More generally, however, if someone gives you a pointer you did not
  33. allocate (or realloc), it's not really possible to figure out how big
  34. the useful portion of that block is.  
  35.  
  36. The Windows API GlobalSize() tells you how big a block was returned,
  37. which is >= the size requested in GlobalAlloc(). 
  38.  
  39. Whoever passes in the pointer must tell you how big the memory is or
  40. provide some identifiable termination marker in the data.  Period.
  41.  
  42.                     --Norm 
  43.  
  44.